package com.luoye.unionlotto;

import java.util.ArrayList;
import java.util.List;

public class LuckFunc
{
    public List<LuckEntity> startlotto(List<Unionlotto> buys,Unionlotto right){
//        System.out.print("开奖号码:");
//        for(String redBall:right.getRedBalls()){
//            System.out.print(redBall+" ");
//        }
//        System.out.println("- "+right.getBlueBall());
        List<LuckEntity> luckEntityList=new ArrayList<>();


        String[] right_ball=new String[7];
        for(int i=0;i<6;i++){
            right_ball[i]=right.redBalls.get(i);
        }
        right_ball[6]=right.blueBall;

        for(Unionlotto unionlotto:buys){
            //System.out.print(unionlotto.getDate()+":");
            String[] input_ball=new String[7];
            for(int i=0;i<6;i++){
                input_ball[i]=unionlotto.redBalls.get(i);
            }
            input_ball[6]=unionlotto.blueBall;
            boolean[] goal_ball = compare_right(input_ball, right_ball);

            Unionlotto unionlotto1=new Unionlotto();
            unionlotto1.redBalls=new ArrayList<>();
            for (int i = 0; i < goal_ball.length; i++)
            {
                //System.out.print(goal_ball[i]+",");
                if (goal_ball[i])
                {
                    if (i < 6)
                    {
                        unionlotto1.redBalls.add(input_ball[i]);
                    }
                    else {
                        unionlotto1.blueBall=input_ball[i];
                    }
                }
            }

            //中奖结果
            String result = judge_goal(goal_ball);

            luckEntityList.add(new LuckEntity(unionlotto1,result));

            //打印中奖结果
//            System.out.print("中奖号码:");
//            for(String redBall:unionlotto1.getRedBalls()){
//                System.out.print(redBall+" ");
//            }
//            System.out.println("- "+unionlotto1.getBlueBall());
//            System.out.println("中奖结果："+result);
        }
        return luckEntityList;
    }
    //判断最后输入的值是否中奖
    public boolean[] compare_right(String[] input_ball, String[] right_choose)
    {
        boolean[] right_ball = new boolean[7];

        //判断蓝色球是否正确
        if (input_ball[6].equals(right_choose[6]))
        {
            right_ball[6] = true;
        }
        else
        {
            right_ball[6] = false;
        }
        for (int i = 0; i < input_ball.length - 1; i++)
        {
            for (int j = 0; j < right_choose.length - 1; j++)
            {
                if (input_ball[i].equals(right_choose[j]))
                {
                    right_ball[i] = true;
                    break;
                }
                else
                {
                    right_ball[i] = false;
                }
            }
        }
        return right_ball;
    }

    //判断获取几等奖,根据网上规则
    public String judge_goal(boolean[] input_goal)
    {
        int red_count = 0;
        int blue_count = 0;
        for (int i = 0; i < input_goal.length - 1; i++)
        {
            if (input_goal[i])
            {
                red_count++;
            }
        }
        if (input_goal[input_goal.length - 1])
        {
            blue_count = 1;
        }
        switch (red_count)
        {
            case 1:
                //return "六等奖";
            case 2:
                if (blue_count == 1)
                {
                    return "六等奖";
                }
                else
                {
                    return "未获奖";
                }
            case 3:
                if (blue_count == 1)
                {
                    return "五等奖";
                }
                else
                {
                    return "未获奖";
                }
            case 4:
                if (blue_count == 1)
                {
                    return "四等奖";
                }
                else
                {
                    return "五等奖";
                }
            case 5:
                if (blue_count == 1)
                {
                    return "三等奖";
                }
                else
                {
                    return "四等奖";
                }
            case 6:
                if (blue_count == 1)
                {
                    return "一等奖";
                }
                else
                {
                    return "二等奖";
                }
            default:
                return "未获奖";
        }
    }
}
